package com.tueye.web.controller.global;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.tueye.base.util.Page;
import com.tueye.entity.Admin;
import com.tueye.service.IAdminInfoService;
import com.tueye.service.global.IGlobalGridService;
import com.tueye.utils.DateUtils;
import com.tueye.utils.TuEyeConstant;
import com.tueye.utils.UUIDGenerator;
import com.tueye.web.controller.BaseController;

/**
 * 用户添加控制层操作demo
 * spring mvc 2.5 支持MultiActionController
 * @author hanxiaowei
 *
 */
@SuppressWarnings("unchecked")
@org.springframework.stereotype.Controller
@RequestMapping("/global.do")
public class GlobalGridController extends BaseController {
	private Log log = LogFactory.getLog(GlobalGridController.class);
	
	@Autowired
	@Qualifier("globalGridService")
	private IGlobalGridService globalGridService;
	
	//@RequestMapping(method = { RequestMethod.POST })
	@RequestMapping(params = "action=PageShow")
	public String PageShow (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// js传过来的参数列表：
		// page:页数
		// rp:每页显示多少条
		// sortname：排序的字段名称
		// sortorder: 排序的方向；比如：asc，desc；
		// query : 查询的条件
		// qtype ：查询的类型
		
		String rp=request.getParameter("rp"); //每页显示的数据数
		String pageString=request.getParameter("page");//获取 flexgrid传过来的页数。
		String query=request.getParameter("query");//获取 查询的条件
	//	query = java.net.URLDecoder.decode(query , "UTF-8");//解码
		String error = request.getParameter("ss");
		String classID = request.getParameter("classID");
	    String classStr = TuEyeConstant.getClassStrByClasId(classID);
	    classStr="com.tueye.entity.Admin";
		//error.indexOf("23");
		
		String qtype=request.getParameter("qtype");//获取 查询的类型
		String sortorder=request.getParameter("sortorder");//获取 查询的类型
		if(pageString == null || pageString.length() == 0) {
			pageString = "1";
		}
		StringBuffer hql = new StringBuffer(" from "+classStr+" ");
		
		if(StringUtils.isNotEmpty(query)) {
			hql.append(" where "+query);
		}
		//if(StringUtils.isNotEmpty(query)) hql.append(" where "+qtype+" ='"+query+"'");
		if(StringUtils.isNotEmpty(qtype)) {
			hql.append("   order   by "+qtype);
		}
		if(StringUtils.isNotEmpty(sortorder)) {
			hql.append("   "+sortorder);
		}
		log.info("############# "+hql);
		Page pageModel = globalGridService.pagedQueryByHQL(hql.toString(), Integer.valueOf(pageString), Integer.valueOf(rp), null);
		
		Map pageInfo = new HashMap();
		pageInfo.put("page", new Integer(pageString));
		pageInfo.put("total", pageModel.getTotalCount());
		//生成json字符串
		String jsonStr = globalGridService.getJSonStr((List)pageModel.getResult(),pageInfo, classStr);
		//getJsonString((List)pageModel.getResult(),pageInfo);
		log.info("###############:"+jsonStr);
		response.setContentType("html/txt");
		response.setCharacterEncoding("utf-8");
		response.getWriter().println(jsonStr);
		
/*		response.setContentType("html/txt");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache, must-revalidate");
		response.setHeader("Pragma", "no-cache");
		response.getWriter().write(jsonStr);
		response.getWriter().flush();
		response.getWriter().close();*/
		
		return null;
	}
	
	/**
	 * 
	 * @param obj
	 * @return
	 */
	@SuppressWarnings("unused")
	private Object getJsonString(Object obj){
		
		JSONObject json = JSONObject.fromObject(obj);
		log.info("############# "+json);
		return json;
	}
	
}
